package com.lyl.mallcloud.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyl.mallcloud.mapper.UsersMapper;
import com.lyl.mallcloud.pojo.Result;
import com.lyl.mallcloud.pojo.Users;
import com.lyl.mallcloud.pojo.VipUserPage;
import com.lyl.mallcloud.service.IUsersService;
import com.lyl.mallcloud.utils.AliOssUtil;
import com.lyl.mallcloud.utils.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author liyunlong
 * @since 2024-05-10
 */
@RestController
@Slf4j
public class UsersController {

    @Autowired
    IUsersService usersService;

    @Autowired
    UsersMapper usersMapper;

    /***
     * @Description: 会员信息的分页展示
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @GetMapping("/vipUserList")
    public Result vipUserList(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "4") Integer pageSize
                       ) {
        List<Users> userItemList;
        VipUserPage vipUserPage = null;
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Users> users = usersMapper.selectList(null);
            PageInfo<Users> pageInfo = new PageInfo<>(users);
            int pages = pageInfo.getPages();
            int pageNum1 = pageInfo.getPageNum();
            userItemList = pageInfo.getList();
            vipUserPage = new VipUserPage();
            vipUserPage.setUsersList(userItemList);
            vipUserPage.setPageNum(pageNum1);
            vipUserPage.setPages(pages);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("会员管理");
        return Result.success(vipUserPage);
    }

    /***
     * @Description: 查看会员信息
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @GetMapping("/vipUserInfo")
    public Result info(@RequestParam(value = "id") Long id) {
        Users user = usersService.findById(id);
        log.info("查看用户信息：{}", user.getUsername());
        return Result.success(user);
    }

    /***
     * @Description: 删除会员
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @DeleteMapping("/deleteVipUser")
    public Result delete(@RequestParam(value = "id") Long id) {
        usersMapper.deleteById(id);
        log.info("删除用户信息：{}", id);
        return Result.success();
    }

    /***
     * @Description: 批量删除会员
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @DeleteMapping("/deleteUserBatch/{ids}")
    public Result deleteUserBatch(@PathVariable("ids") List<String> ids) {
            usersMapper.deleteBatchIds(ids);
            log.info("批量删除会员信息：{}", ids);
            return Result.success();
    }


    /***
     * @Description: 保存会员信息
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @PostMapping("/users/save")
    @ResponseBody
    public Result createUser(@RequestBody Users userData) {
        usersService.save(userData);
        return Result.success();
    }

    @PostMapping("/users/get/{userId}")
    @ResponseBody
    public Result getUserById2(@PathVariable("userId") Long userId) {
        Users user = usersService.getById(userId);
        return Result.success(user);
    }

    /***
     * @Description: 新增会员
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */

    @PostMapping("/addVipUser")
    public Result addVipUser(@RequestBody Users users) {
        if (users.getUsername()==null){
            return Result.error("会员的名字呢");
        }
        boolean save = usersService.save(users);
        if (save) {
            log.info("新增用户：{}", users.getUsername());
            return Result.success();
        } else {
            log.info("新增用户失败");
            return Result.error("添加会员失败");
        }
    }




    /***
     * @Description: 更新操作
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @PostMapping("/updateVipUser")
    public Result updateUser(@RequestBody Users users) {
        usersService.updateById(users);
        log.info("修改会员信息成功");
        return Result.success();

    }

    /***
     * @Description: 导出会员信息
     * @Author: Liyunlong
     * @Date: 2024/5/12
     */
    @GetMapping("/exportUser")
    public void exportUser(HttpServletResponse response) throws IOException {
        String excelName = "会员信息";
        List<Users> list = usersMapper.selectList(null);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String fileName = excelName + now.format(formatter);
        log.info("导出会员信息，文件名：{}", fileName);
        ExcelUtils.exportExcel(new ArrayList<>(list), excelName, excelName, Users.class, fileName, response);
    }
}